package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 通用接口
 * @author : 黑渊白花
 * @ClassName CommonController
 * @date : 2024/2/29 13:02
 * @Description
 */

@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api(tags = "通用接口")
public class CommonController {

    @Value("${files.upload.path}")
    private String fileUploadPath;
    @Value("${files.upload.url}")
    private String host;

    /**
     * 文件上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(@RequestParam MultipartFile file) throws IOException {
        log.info("文件上传");
        //文件原始名称
        String originalFilename = file.getOriginalFilename();
        log.info("原来名称：{}",originalFilename);
        int index = originalFilename.lastIndexOf(".");
        //文件类型
        String extname = originalFilename.substring(index);
        //文件名
        String newFileName = UUID.randomUUID().toString() + extname;
        log.info("新名称：{}",newFileName);
        String newFilePath = fileUploadPath+newFileName;
        log.info("存储路径：{}",newFilePath);
        file.transferTo(new File(newFilePath));
        return Result.success(host+newFileName);
    }


}
